import React, { Component } from "react";
import { BrowserRouter, Route, Prompt } from "react-router-dom";
import Home from "./header/logo";
import Login from "./login";
import Login1 from "./login1";
import Pass from "./pass";
import Register from "./register";
import Classify from "./classify/index";
import List from "./list/index";
import Finish from "./finish/index";
import News from "./news/index";
import Welfare from "./welfare/welfare";
import { Modal } from "antd";

export default class index extends Component {
  success = data => {
    Modal.success({
      content: data
    });
  };
  render() {
    return (
      <BrowserRouter>
        <Route path="/" exact component={Home} />
        <Route path="/login" component={Login} />
        <Route path="/login/login" component={Login1} />
        <Route path="/login/account" component={Login1} />
        <Route path="/pass" component={Pass} />
        <Route path="/register" component={Register} />
        <Route path="/classify" component={Classify} />
        <Route
          children={props => {
            return (
              <Prompt
                message={location => {
                  // location表示进入下一个路由的信息对象
                  if (location.pathname === "/list") {
                    // 从全局里获取用户的登录信息
                    const user = window.sessionStorage.getItem("user");
                    if (user) {
                      return true;
                    } else {
                      this.success("对不起，您还没有登陆，所以不能查看！");
                      props.history.replace("/login/login");
                      return false;
                    }
                  }
                  return true;
                }}
              />
            );
          }}
        />
        <Route path="/list" component={List} />
        <Route path="/finish" component={Finish} />
        <Route path="/news" component={News} />
        <Route path="/welfare" component={Welfare} />
      </BrowserRouter>
    );
  }
}
